<?php
	require ("imdbsearch.class.php");

	if(!file_exists("/Users/".$_ENV["USER"]."/Library/Caches/com.tegosaurus.appletvdatamaker/xml/")){
		umask(0);
		mkdir("/Users/".$_ENV["USER"]."/Library/Caches/com.tegosaurus.appletvdatamaker/xml/");
	}


	if(isset($argv[1])){ // File is given
		$movfile = substr($argv[1], strrpos($argv[1], "/")+1);
		$movdir  = substr($argv[1], 0, strrpos($argv[1], "/"));
		$ext = strtolower(substr($movfile, strrpos($movfile, ".")+1));

		$title = substr($movfile, 0, strlen($movfile) - strlen($ext) - 1);
		// Now clean "parts" options - based on ATVFiles default config (wiki.akwardtv.org)
		$title = preg_replace("/[_\.-]+cd[_\.-]*([0-9a-d]+)/","",$title);
		$title = preg_replace("/[_\.-]+dvd[_\.-]*([0-9a-d]+)/","",$title);
		$title = preg_replace("/[_\.-]+part[_\.-]*([0-9a-d]+)/","",$title);
		$title = preg_replace("/[_\.-]*sample[_\.-]*([0-9a-d]+)/","",$title);
		$title = preg_replace("/[_\.-]*trailer[_\.-]*([0-9a-d]+)/","",$title);

		// Do we have it in cache?
		$xml = "";
		if(file_exists("/Users/".$_ENV["USER"]."/Library/Caches/com.tegosaurus.appletvdatamaker/xml/".$title.".xml")){
			$temp = file("/Users/".$_ENV["USER"]."/Library/Caches/com.tegosaurus.appletvdatamaker/xml/".$title.".xml");
			$xml = implode("",$temp);
		}else{
			if($argv[2] != 0){
				$argv[2] = str_replace("t","",$argv[2]);
				$movie = new imdb($argv[2]);
				$movieid = $argv[2];

				$movie->setid ($movieid);
				$cols = $movie->colors ();
				$sound = $movie->sound ();
				$country = $movie->country ();

				if(!is_array($cols)){ $cols = array($cols); }
				if(!is_array($akas)){ $akas = array($akas); }
				if(!is_array($country)){ $country = array($country); }
				if(!is_array($sound)){ $sound = array($sound); }

				$xml = '<media type="Movie">
	<title>'.$movie->title().'</title>
	<description><![CDATA[';
				$plot = $movie->plot (); 
				$xml .= preg_replace("/\<i\>.+?Written.*/s","",str_replace("\n","",$plot[0]));
				$xml .= ']]></description>
';
				$certs = $movie->mpaa();
				if(isset($certs["Belgium"])){ $xml .= '	<rating>'.$certs["Belgium"].' (Belgium)</rating>'."\n"; }
				elseif(isset($certs["USA"])){ $xml .= '	<rating>'.$certs["USA"].'</rating>'."\n"; }

				$rating = $movie->rating();
				$xml .= '	<starRating>'.round($rating/2)."</starRating>\n";
				$xml .= '	<imdbRating>'.$rating."</imdbRating>\n";
				$xml .= '	<imdbnr>'.$argv[2]."</imdbnr>\n";
				$xml .= '	<published>'.$movie->year()."-01-01</published>\n";
				$xml .= '	<genres>'."\n";
				$gen = $movie->genres ();
				$setgenres = array();
				$i=0;
				foreach($gen as $genre){
					if(in_array($genre, $setgenres)){ continue; }
					$xml .= '		<genre'.(($i==0)?' primary="true"':'').'>'.$genre.'</genre>'."\n";
					array_push($setgenres, $genre);
					$i++;
				}
				$xml .= "\t</genres>\n";
				$xml .= "\t<cast>\n";
				$cast = $movie->cast();
				foreach($cast as $member){
					$xml .= '		<name><![CDATA['.$member["name"].']]></name>'."\n";
				}
				$xml .= "\t</cast>\n";
		
				if(!isset($argv[3]) or ($argv[3] != "chris")){
					$xml .= "\t<producers>\n";
					$cast = $movie->producer();
					foreach($cast as $member){
						$xml .= '		<name><![CDATA['.$member["name"].']]></name>'."\n";
					}
					$xml .= "\t</producers>\n";
					$xml .= "\t<directors>\n";
					$cast = $movie->director();
					foreach($cast as $member){
						$xml .= '		<name><![CDATA['.$member["name"].']]></name>'."\n";
					}
					$xml .= "\t</directors>\n";
					$xml .= "\t<authors>\n";
					$cast = $movie->writing();
					foreach($cast as $member){
						$xml .= '		<name><![CDATA['.$member["name"].']]></name>'."\n";
					}
					$xml .= "	</authors>\n";
				}
		
				$xml .= "</media>";
			}
		}

		if($xml != ""){
			// Wegschrijven...
			if(false !== strpos($movtitle,"sample")){
				$txml = str_replace("</title>"," / Sample</title>", $xml);
			}else if(false !== strpos($movtitle,"trailer")){
				$txml = str_replace("</title>"," / Trailer</title>", $xml);
			}else{
				$txml = $xml;
			}
			
			$temp = fopen(str_replace($ext,"xml",$argv[1]),"w");
			fwrite($temp, $txml);
			$temp = fopen("/Users/".$_ENV["USER"]."/Library/Caches/com.tegosaurus.appletvdatamaker/xml/".$title.".xml","w");
			fwrite($temp, $txml);
			echo '1';
			exit;
		}
	}
	echo '2';
?>
